﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using HHPS.CreditEducation.Presentation.Helps;
using HHPS.CreditEducation.Presentation.Materials;
using HHControls.CreditEducation;
using HHPS.CreditEducation.Presentation.StudyPlans;

namespace HHPS.CreditEducation.Presentation
{
    #region Navigation

    public class Navigation
    {
        public const int ItemWidth = 150;
        public const int ItemHeight = 30;
        public const int StartX = 20;
        public const int StartY = 39;

        public static List<NavGroup> NavGroups = new List<NavGroup>();
        public static List<NavItem> NavItems = new List<NavItem>();

        public static void Initialize()
        {
            NavItems.Clear();
            NavGroups.Clear();

            NavItems.Add(new NavItem { Name = "QL kế hoạch", GroupName = "expStudyPlans", ShowingControl = new ManagementControl() });

            NavItems.Add(new NavItem { Name = "Người dùng", GroupName = "expMaterials", ShowingControl = new PersonalInfoControl() });
            NavItems.Add(new NavItem { Name = "Chương trình khung", GroupName = "expMaterials", ShowingControl = new SyllabusControl() });
            NavItems.Add(new NavItem { Name = "Thông tin môn học", GroupName = "expMaterials", ShowingControl = new SubjectControl() });
            NavItems.Add(new NavItem { Name = "Thông tin chung", GroupName = "expMaterials", ShowingControl = new UniversityControl() });

            NavItems.Add(new NavItem { Name = "Hướng dẫn", GroupName = "expHelps", ShowingControl = new ViewHelpControl() });
            NavItems.Add(new NavItem { Name = "Tác giả", GroupName = "expHelps", ShowingControl = new AboutForm() });

            NavGroups.Add(new NavGroup { Text = "Trợ giúp", Name = "expHelps" });
            NavGroups.Add(new NavGroup { Text = "Tài nguyên", Name = "expMaterials" });
            NavGroups.Add(new NavGroup { Text = "Kế hoạch", Name = "expStudyPlans" });
        }

        private static void AddItemToGroup()
        {
            var startPoint = new Point(StartX, StartY);

            foreach (var navGroup in NavGroups)
            {
                var currentGroupName = navGroup.Name;
                foreach (var navItem in NavItems.Where(n => n.GroupName == currentGroupName))
                {
                    navGroup.Instance.Controls.Add(navItem.Instance);
                    navItem.Instance.Location = new Point(startPoint.X, startPoint.Y + (ItemHeight + 1) * (navGroup.Instance.Controls.Count - 1));
                }

                navGroup.Instance.ExpandedSize = startPoint.Y + (ItemHeight + 1) * navGroup.Instance.Controls.Count;
            }
        }

        private static void AddGroupToControlCollection(Control.ControlCollection controlCollection)
        {
            foreach (NavItem navItem in NavItems)
            {
                NavItem item = navItem;
                navItem.Instance.Click += (s, e) => ShowControlOnPanel(item);
            }

            controlCollection.Clear();
            foreach (NavGroup navGroup in NavGroups)
            {
                controlCollection.Add(navGroup.Instance);

                NavGroup currentGroup = navGroup;
                navGroup.Instance.Click += (s, e) => ExpandGroup(currentGroup);
                navGroup.Instance.MouseHover += (s, e) => ExpandGroup(currentGroup);
            }
        }

        public static void ExpandGroup(NavGroup currentGroup)
        {
            foreach (NavGroup otherGroup in NavGroups.Where(otherGroup => otherGroup.Name != currentGroup.Name))
            {
                otherGroup.Instance.IsExpand = false;
            }

            if (!currentGroup.Instance.IsExpand)
            {
                currentGroup.Instance.IsExpand = true;
            }
        }

        public static void ShowControlOnPanel(NavItem item)
        {
            if (item.ShowingControl == null) return;

            if (item.ShowingControl is Form)
            {
                if (item.ShowingControl.IsDisposed)
                {
                    item.ShowingControl = (Form)Activator.CreateInstance(item.ShowingControl.GetType());
                }
                ((Form)item.ShowingControl).ShowDialog();
            }
            else if (item.ShowingControl is UserControl)
            {
                NavItem navItem = NavItems.FirstOrDefault(i => i.Instance.IsClicked);
                if (navItem != null)
                {
                    navItem.Instance.IsClicked = false;
                }
                item.Instance.IsClicked = true;

                // This function win re-create instance of Control
                if (item.ShowingControl is ElementControl)
                {
                    item.ShowingControl = (Control)Activator.CreateInstance(item.ShowingControl.GetType(), item.Name);
                }
                else
                {
                    item.ShowingControl = (Control)Activator.CreateInstance(item.ShowingControl.GetType());
                }

                MainView.Instance.AddUserControlToPanel(item.ShowingControl);
            }
        }

        public static void Show(Control.ControlCollection controlCollection)
        {
            //Initialize();
            AddItemToGroup();
            AddGroupToControlCollection(controlCollection);
        }
    }

    #endregion

    #region Nest Classes

    public class NavGroup
    {
        public NavGroup()
        {
            Instance = new HHArrowExpand { Dock = DockStyle.Top, IsExpand = false };
        }

        public HHArrowExpand Instance { get; private set; }
        public string Text
        {
            get { return Instance.TextTitle; }
            set { Instance.TextTitle = value; }
        }

        public string Name { get; set; }
    }

    public class NavItem
    {
        public NavItem()
        {
            Instance = new HHSimpleButton
                           {
                               Width = Navigation.ItemWidth,
                               Height = Navigation.ItemHeight,
                               Font = new Font("Tahoma", 10)
                           };
        }

        public HHSimpleButton Instance { get; private set; }
        public string Name
        {
            get { return Instance.Text; }
            set { Instance.Text = value; }
        }
        public string GroupName { get; set; }
        public Control ShowingControl { get; set; }
        //public string Text { get; set; }
    }

    #endregion
}
